<template>
	<view class="haha">
		<view class="novel" @click="onPageScroll()">
			<p v-html="novel"></p>
		</view>
		<view class="wrap" v-if="isShow">
			<!-- <u-back-top :scrollTop="scrollTop" :mode="mode" :iconStyle="iconStyle" :customStyle="{backgroundColor:'#fe8761'}"></u-back-top> -->
			<text @click="upList()">上一章</text>
			<text @click="nextList()">下一章</text>
		</view>
	</view>
</template>

<script>
	import {
		getNovel
	} from '@/api/index.js';
	export default {
		data() {
			return {
				novel: '',
				scrollTop: 0,
				mode: 'circle',
				iconStyle: {
					fontSize: '32rpx',
					color: '#fff',
				},
				isShow: false,
				novelList: [],
				novelKey: 0,
				source_uuid: '',
				timer: null,
				num: 0,
				author: '',
				category: '',
				description: '',
				img: '',
				title: '',
				readList: {},
				book_id: '',
				readBook: []
			}
		},
		onLoad(options) {
			uni.showLoading({
				title: '内容加载中...'
			})
			console.log(JSON.parse(options.data));
			let data = JSON.parse(options.data);
			let {
				source_uuid,
				uuid,
				arr,
				key,
				author,
				category,
				description,
				img,
				title
			} = data;
			this.readList.book_sourceUuid = source_uuid;
			this.readList.book_category = category;
			this.readList.book_title = title;
			this.readList.book_author = author;
			this.readList.book_img = img;
			this.readList.book_description = description;
			this.novelList = arr;
			this.novelKey = key;
			this.source_uuid = source_uuid
			getNovel(source_uuid, uuid).then(content => {
				console.log(content);
				let {
					data
				} = content;
				this.novel = data.content;
				//调用云函数，查询阅读
				uniCloud.callFunction({
						name: 'list_read',
						data: {}
					})
					.then(content => {
						console.log('1.0', content);
						let {
							result: {
								data
							}
						} = content;
						this.readBook = data;
						if (data.length == 0) {
							//调用云函数，添加阅读
							uniCloud.callFunction({
									name: 'read',
									data: {
										...this.readList
									}
								})
								.then(content => {
									console.log('1.0', content);
								})
						} else {
							let k = data.length
							if (this.readList.book_sourceUuid == this.readBook[k - 1].book_sourceUuid) {
								return
							} else {
								//调用云函数，添加阅读
								uniCloud.callFunction({
										name: 'read',
										data: {
											...this.readList
										}
									})
									.then(content => {
										console.log('1.0', content);
									})
							}

						}
					})
				let run = uni.getStorageSync('runTime')
				if(run == ''){
					this.timer = setInterval(()=>{
						this.num++;
						uni.setStorageSync('runTime',this.num)
						console.log('1.0',this.num);
					},1000)
				}else{
					this.timer = setInterval(()=>{
						run++
						console.log('2.0',run);
						uni.setStorageSync('runTime',run)
					},1000)
				}
				uni.hideLoading()
			}).catch((error => {
				console.log(error);
			}))


		},
		onUnload() {
			clearTimeout(this.timer);
			this.timer = null;
			console.log('页面离开了');
		},
		methods: {
			onPageScroll(e) {
				this.scrollTop = e.scrollTop;
				// console.log(e);
				if (e.scrollTop > 600) {
					this.isShow = true
				} else {
					this.isShow = false
				}
			},
			async upList() {
				this.novelKey--;
				if (this.novelKey < 0) {
					uni.showToast({
						title: '已经是最前的一章',
						icon: 'none'
					})
					return
				}
				let id = this.novelList[this.novelKey].secId
				uni.showLoading({
					title: '内容加载中...'
				})

				await getNovel(this.source_uuid, id).then(content => {
					console.log(content);
					let {
						data
					} = content;
					// console.log(data.content);
					this.novel = data.content
					uni.hideLoading()
					window.scrollTo(0, 0)
				}).catch((error => {
					console.log(error);
				}))
			},
			async nextList() {
				this.novelKey++;
				let id = this.novelList[this.novelKey].secId
				uni.showLoading({
					title: '内容加载中...'
				})

				await getNovel(this.source_uuid, id).then(content => {
					console.log(content);
					let {
						data
					} = content;
					// console.log(data.content);
					this.novel = data.content
					uni.hideLoading()
					window.scrollTo(0, 0)
				}).catch((error => {
					console.log(error);
				}))
			}
		}

	}
</script>

<style lang="scss">
	.haha {
		width: 100%;
		position: relative;
		background-color: #fceccc;
		height: 600vh;

		.novel {
			background-color: #fceccc;
			padding: 20rpx 40rpx;
			margin-bottom: 100rpx;
			font-size: 14px;

			p {
				line-height: 2.8 !important;
			}
		}

		.wrap {
			width: 100%;
			height: 100rpx;
			background-color: #f0cead;
			position: fixed;
			bottom: 0;
			left: 0;
			display: flex;
			justify-content: space-evenly;
			align-items: center;
		}
	}
</style>